import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:wch/constant/resource_constant.dart';
import 'package:wch/utils/log_utils.dart';

class StageSubPage extends StatefulWidget {
  const StageSubPage({super.key, required this.datas, required this.ratio, required this.yAsix});

  final List<int> datas;

  final double yAsix;

  final double ratio;

  @override
  State<StageSubPage> createState() => _StageSubPageState();
}

class _StageSubPageState extends State<StageSubPage> {

  @override
  Widget build(BuildContext context) {
    return ClipRect(
      child: Container(
        height: 200.h,
        child: CustomPaint(
          painter: CurvePaint(widget.datas, widget.ratio, widget.yAsix),
        ),
      ),
    );
  }
}
class CurvePaint extends CustomPainter {
  List<Paint> painters = [];
  Paint linePainter = Paint()
    ..color = themeColor
    ..strokeCap = StrokeCap.round
    ..strokeWidth = 0.5.w
    ..strokeJoin = StrokeJoin.bevel
    ..style = PaintingStyle.stroke;

  List<int> datas = [];

  double ratio = 1;

  double yAsix = 200;

  CurvePaint(this.datas, this.ratio, this.yAsix);

  @override
  void paint(Canvas canvas, Size size) {
    // TODO: implement paint
    double width = size.width;
    double height = size.height;

    double dataWid = width / (datas.length - 1);
    double dataHeight = height / yAsix / ratio;//-100到100

    Path path2 = Path();
    for (int i = 0; i < datas.length; i++) {
      double data = datas[i] + yAsix/2;
      data = yAsix - data;
      double y2 = data/yAsix*height;
      // if (y2 > 0) {
      //   y2 = min(y2, height);
      // } else {
      //   y2 = max(y2, 0);
      // }

      double x2 = dataWid * i;

      if (i == 0) {
        path2.moveTo(x2, y2);
      }
      else {
        int data = datas[i - 1];
        double y1 = data * dataHeight;
        double x1 = dataWid * (i - 1);

        double cx = (x1 + x2) / 2;
        double cy = (y1 + y2) / 2;
        if (cy > 0) {
          cy = min(cy, height);
        } else {
          cy = max(cy, 0);
        }

        // path2.cubicTo(x1, cy, cx, y2, x2, y2);
        path2.lineTo(x2, y2);
      }
    }

    canvas.drawPath(path2, linePainter);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    // TODO: implement shouldRepaint
    return false;
  }
}
